using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX;
using NovodexWrapper;
using OpenPanekitEditor;
using OpenPanekitEditor.Panel;
namespace OriginalPanels
{
    [PanelAttribute("Original::Shooter", IsDeadEnd = true, LocalD3DModelName = "Model\\Shooter_P.X", SlaveD3DModelNames = new string[1] { "Model\\Shooter_A.X" })]
    public class Shooter : PanelBase, IPanel
    {
        public Image IconImage { get { return Properties.Resources.ElementIcon_Shooter; } }
        public Shooter()
        {
            ParameterDictionary[typeof(ShooterParameter_BulletEnergy)] = new ShooterParameter_BulletEnergy();
        }
        Matrix l_to_r_matrix = Matrix.Identity;
        public Matrix LocalToRootMatrix
        {
            get { return l_to_r_matrix; }
            set { l_to_r_matrix = value; }
        }
        public Matrix PivotToRootMatrix
        {
            get { return Matrix.Zero; }
            set { }
        }

        public Matrix PivotMatrix
        {
            get { return Matrix.Zero; }
            set { }
        }
        NxActor parent_actor;
        public override NxActor InitNx(NxScene scene, NxActor parent, Matrix pmatrix)
        {
            parent_actor = parent;
            CreatePanelShape(parent, pmatrix);
            return null;
        }
        float energy;
        public override void UpdateNxParameter(float dt)
        {
            energy += dt*ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].UpperLimit;
            if (energy > ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].UpperLimit) energy = ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].UpperLimit;
        }
        public override NxActor GetSlaveNxActor(NxScene scene)
        {
            if (ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].Value > 0)
            {
                if (ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].Value > energy) return null;
                energy -= ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].Value;

                 Vector3 createpos = new Vector3(0, 0, OpenPanekitEditor.Panel.PanelUtil.PanelUnit * 0.9f);
                NxActor bactor = NovodexUtil.createSphereActor(scene, 0.05f,Matrix.Translation(createpos) * l_to_r_matrix * parent_actor.getGlobalPose(), 1000);
                bactor.addLocalForceAtLocalPos(new Vector3(0, 0, ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].Value), Vector3.Empty, NxForceMode.NX_IMPULSE);
                createpos = Vector3.TransformCoordinate(createpos, l_to_r_matrix);
                Vector3 force = Vector3.TransformNormal(new Vector3(0,0,-ParameterDictionary[typeof(ShooterParameter_BulletEnergy)].Value),l_to_r_matrix);
                parent_actor.addLocalForceAtLocalPos(force, createpos, NxForceMode.NX_IMPULSE);
                return bactor;
            }
            return null;
        }
    }
    public class ShooterParameter_BulletEnergy : OpenPanekitEditor.Panel.Parameter
    {
        public override float UpperLimit
        {
            get { return 50f; }
        }
        public override float LowerLomit
        {
            get { return 1f; }
        }
        public override float Quantize
        {
            get { return 1f; }
        }
        public override String Title
        {
            get { return Resouces.Strings.SettingParameter_ShooterBulletEnergy; }
        }
        public override String Unit
        {
            get { return "J"; }
        }
        public override bool CanAssignControl
        {
            get { return true; }
        }
    }
    public class ShooterParameter_FireRate : OpenPanekitEditor.Panel.Parameter
    {
        public override float UpperLimit
        {
            get { return 10f; }
        }
        public override float LowerLomit
        {
            get { return 0f; }
        }
        public override float Quantize
        {
            get { return 0.1f; }
        }
        public override String Title
        {
            get { return Resouces.Strings.SettingParameter_ShooterFireRate; }
        }
        public override String Unit
        {
            get { return "Fire/Sec"; }
        }
        public override bool CanAssignControl
        {
            get { return false; }
        }
    }
}